/**
 * 
 */
package org.nielin.bestwaper.core;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nielin.util.log.MyLogger;

/**
 * @author rockydai
 *
 */
public class View extends WebEntity {
	
	private String absoluteUrl;
	
	public View( String id, String url ) {
		this.id = id;
		absoluteUrl = url;
	}
    
    public String getForwardUrl()
    {
        return absoluteUrl;
    }
	
	public void go(WebEntityContext context) {
        if ( !validate(context) )
        {
            return;
        }
        
        HttpServletRequest req = context.getRequest();
        HttpServletResponse res = context.getResponse();
		try
		{
			String forwardURL = absoluteUrl;
			javax.servlet.RequestDispatcher dispatcher = req.getRequestDispatcher(forwardURL);			
			dispatcher.forward(req, res);
		}
		catch(Exception e)
		{
		    String id = getId();
		    if (id.equals(new ConstantsBean().ERROR_REDIRECT_ID))
		    {
		        MyLogger.getNormal().error("view[" + new ConstantsBean().ERROR_REDIRECT_ID + "]出现异常");
		        try {
		            PrintWriter out = res.getWriter();
		            out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
		                "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.xml\">\n" +
		                "<wml>\n" +
		                "<head>\n" +
		                "<meta http-equiv=\"Cache-control\" content=\"max-age=0\" />\n" +
		                "<meta http-equiv=\"Cache-control\" content=\"no-cache\" />\n" +
		                "</head>\n" +
		                "<card>\n" +
		                "<p>view[error]出现异常</p>\n" +
		                "<p>\n" +
		                "<anchor><prev/>返回上一页</anchor>" +
		                "</p>\n" +
		                "</card>\n" +
		                "</wml>");
		        }
		        catch (Exception e2) {
		        }

		        return;
		    }
		    ErrorPageBean errorpage = new ErrorPageBean("页面 id[" + id + "]跳转失败！");
            MyLogger.getNormal().error( "[" + errorpage.getErrno() + "]view [" + getId() + "] go fail ", e);
            Dispatcher.toErrorPage(req, res, errorpage);
            return;
		}		
	}

}
